package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("addressBook")
public class AddressBookController{
    @Autowired
    private AddressBookService addressBookService;
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook, HttpSession httpSession){
        //获取客户sessionid
        String userId = httpSession.getAttribute("user").toString();
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> addressBooks = addressBookService.list(lambdaQueryWrapper);
        log.info("展示了的地址：{}",addressBooks);
        return R.success(addressBooks);
    }
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);

    }

    @DeleteMapping
    public R<String> delete(Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }

    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook,HttpSession httpSession){
        Long userId = (Long) httpSession.getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        log.info("添加的地址为：{}",addressBook);
        return R.success("成功添加");
    }
    @PutMapping("/default")
    public R<String> updateIsDefault(@RequestBody AddressBook addressBook,HttpSession httpSession){
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Long userId = (Long) httpSession.getAttribute("user");
        lambdaQueryWrapper.in(AddressBook::getUserId,userId);
        List<AddressBook> addressBooks = addressBookService.list(lambdaQueryWrapper);
        addressBooks.stream().map((item)->{
            item.setIsDefault(0);
            if (item.getId().equals(addressBook.getId())){
                item.setIsDefault(1);
            }
            return item;
        }).collect(Collectors.toList());
        addressBookService.updateBatchById(addressBooks);

        return R.success("成功修改");
    }
    @GetMapping("/default")
    public R<AddressBook> getDefault(AddressBook addressBook,HttpSession httpSession){
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .eq(AddressBook::getIsDefault,1);
        AddressBook bookServiceOne = addressBookService.getOne(lambdaQueryWrapper);
        if (addressBook == null){
            return R.error("没有找到默认地址");
        }else {
            return R.success(bookServiceOne);
        }



    }

    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Long id = addressBook.getId();
        lambdaQueryWrapper.eq(AddressBook::getId,id);
        addressBookService.update(addressBook,lambdaQueryWrapper);
        log.info("成功修改为：{}",addressBook);
        return R.success("成功修改");
    }

}
